<template>
  <router-view />
  <GlobalErrorMessage ref="errorMessage" />
  <GlobaldialogOpen ref="dialogOpen" />
  <GlobalxinpuMessage ref="xinpuMessage" />
</template>

<script setup>
import router from '@/router/index.js'
import { ref } from 'vue'
import { ElNotification, ElMessage, ElMessageBox } from 'element-plus'
import useSettingsStore from '@/store/modules/settings'
import { handleThemeStyle } from '@/utils/theme'
import Cookies from 'js-cookie'
import { useCssVar } from '@vueuse/core'
const errorMessage = ref('')
const dialogOpen = ref('')
const xinpuMessage = ref('')
// 变浅颜色值
const getLightColor = (color, level) =>{
	let rgb = hexToRgb(color)
	for (let i = 0; i < 3; i++) {
		rgb[i] = Math.floor((255 - rgb[i]) * level + rgb[i])
	}
	return rgbToHex(rgb[0], rgb[1], rgb[2])
}

// 变深颜色值
const getDarkColor = (color, level) =>{
	let rgb = hexToRgb(color)
	for (let i = 0; i < 3; i++) {
		rgb[i] = Math.floor(rgb[i] * (1 - level))
	}
	return rgbToHex(rgb[0], rgb[1], rgb[2])
}

// hex颜色转rgb颜色
const hexToRgb = (str) =>{
	str = str.replace('#', '')
	let hexs = str.match(/../g)
	for (let i = 0; i < 3; i++) {
		hexs[i] = parseInt(hexs[i], 16)
	}
	return hexs
}

const rgbToHex = (r, g, b) =>{
	let hexs = [r.toString(16), g.toString(16), b.toString(16)]
	for (let i = 0; i < 3; i++) {
		if (hexs[i].length == 1) {
			hexs[i] = `0${hexs[i]}`
		}
	}
	return `#${hexs.join('')}`
}

const errorMessageOpen = ((data,content)=>{
	errorMessage.value.showPrompt(data,content)
})

const xinpuMessageOpen = (async(data,centent)=>{
  const value = await xinpuMessage.value.showPrompt(data,centent);
  // console.log(value);
  // value = value.replace(/["']/g, '');
  console.log(value);
  return value
})

const xinpuWebsApi = (async(method,data)=>{
  let res = null
  if(data == null){
	res = await boundAsync.call(`{"method":"${method}","jyfl":"WebPageStaticCall","data":${null}}`)
  }else{
	res = await boundAsync.call(`{"method":"${method}","jyfl":"WebPageStaticCall","data":${JSON.stringify(data)}}`)
  }
   
	res = JSON.parse(res)
	if(res.Flag == false){
		window.errorMessageOpen(
			"请求异常",
			res.CodeMsg
		);
	}else{
		return res
	}
})

const xinpuHttpApi = (async(method,data,jyfl)=>{
	let res = null
	let jyfls = ''
	
	if(jyfl){
		jyfls = jyfl
		
	}else{
		
		jyfls = "httphosptalserver"
	}
	console.log(`{"method":"${method}","jyfl":"${jyfls}","data":${JSON.stringify(data)}}`);
	if(data == null){
		res = await boundAsync.call(`{"method":"${method}","jyfl":"${jyfls}","data":${data}}`)
	}else{
		res = await boundAsync.call(`{"method":"${method}","jyfl":"${jyfls}","data":${JSON.stringify(data)}}`)
	}
	res = JSON.parse(res)
	if(res.Flag == false){
		window.errorMessageOpen(
			"请求异常",
			res.CodeMsg
		);
	}else{
		return res
	}
})

const xinpuHttpApi1 = (async(method,data)=>{
	let res = null
	// console.log(`{"method":"${method}","jyfl":"dzblhttphosptalserver","data":${JSON.stringify(data)}}`);
	if(data == null){
		res = await boundAsync.call(`{"method":"${method}","jyfl":"zyhttphosptalserver","data":${data}}`)
	}else{
		res = await boundAsync.call(`{"method":"${method}","jyfl":"zyhttphosptalserver","data":${JSON.stringify(data)}}`)
	}
	// console.log( `我是app.vue里面http入参
	// {"method":"${method}","jyfl":"dzblhttphosptalserver","data":${JSON.stringify(data)}}`);
	res = JSON.parse(res)
	if(res.Flag == false){
		window.errorMessageOpen(
			"请求异常",
			res.CodeMsg
		);
	}else{
		return res
	}
})
 
const xinpudialogOpen = (async(data)=>{
	const value = await dialogOpen.value.showPrompt(data);
  // value = value.replace(/["']/g, '');
  console.log(value);
  return value
})
const handleClose = () =>{
	dialogVisible.value = false
}
onMounted(() => {
	
//   xinpuMessageOpen('warning','sdfasfsfd十多个十多个撒gassdfasfsfd十多个十多个撒gassdfasfsfd十多个十多个撒gassdfasfsfd十多个十多个撒gas')
  CefSharp.BindObjectAsync("boundAsync");
  window.errorMessageOpen = errorMessageOpen;
  window.xinpuMessageOpen = xinpuMessageOpen;
  window.xinpudialogOpen = xinpudialogOpen;
  window.xinpuWebsApi = xinpuWebsApi;
  window.xinpuHttpApi = xinpuHttpApi;
  window.xinpuHttpApi1 = xinpuHttpApi1;
   
  nextTick(() => {
    // 初始化主题样式
    handleThemeStyle(useSettingsStore().theme)
    const sizeValue = useCssVar('--size')
    sizeValue.value = `${Cookies.get('sizePm') || 14}px`
    let theme = Cookies.get('QJColor') || '#409EFF'
    document.documentElement.style.setProperty('--el-color-primary', theme)
    document.documentElement.style.setProperty('--el-menu-active-color', theme)
    for (let i = 1; i <= 9; i++) {
      document.documentElement.style.setProperty(`--el-color-primary-light-${i}`, `${getLightColor(theme, i / 10)}`)
    }
    for (let i = 1; i <= 9; i++) {
      document.documentElement.style.setProperty(`--el-color-primary-dark-${i}`, `${getDarkColor(theme, i / 10)}`)
    }
  })
})
</script>
<style lang="scss">
:root {
  --size: 16px;
}
</style>
